package org.example;

import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.mapper.StudentMapper;
import org.example.mapper.UserMapper;
import org.example.pojo.Student;
import org.example.pojo.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//通过模糊查询学生信息
public class MyBatisDemo5 {
    public static void main(String[] args) throws IOException {
        //加载mybatis核心配置文件 获取sqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //获取sqlSession对象 用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //通过模糊查询学生信息
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        //方式一
        //List<Student> students = studentMapper.selectByCondition("%李%","%计算机%");
        //方式二
        Student student = new Student();
        student.setRealname("%李%");
        student.setDepart("%计算机%");
        //List<Student> students = studentMapper.selectByCondition(student);
        //方式三
        Map map = new HashMap();
        map.put("realname","%李%");
        map.put("depart","%计算机%");
        List<Student> students = studentMapper.selectByCondition(map);
        System.out.println(students);
        sqlSession.close();
    }
}
